<template>

  <div>
    <!--搜索框部分-->
    <el-card>

      <el-form inline label-width="90px" size="mini" class="demo-form-inline">

        <el-form-item label="物品名称">
          <el-input placeholder="请输入物品名称" v-model.trim="searchShoppingCart.name" clearable size="mini"></el-input>
        </el-form-item>

        <el-form-item label="商品类型">
          <el-select v-model="searchShoppingCart.typeId" placeholder="请选择商品类型" clearable>
            <el-option
              v-for="item in shoppingCartTypeList"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="商品详情">
          <el-input placeholder="请输入商品详情" v-model.trim="searchShoppingCart.details" clearable size="mini"></el-input>
        </el-form-item>

        <el-form-item label="评分">
          <el-select v-model="searchShoppingCart.grade" placeholder="请选择评价" clearable>
            <el-option label="☆ 浪费时间" :value="1"></el-option>
            <el-option label="☆☆ 勉强下咽" :value="2"></el-option>
            <el-option label="☆☆☆ 一般" :value="3"></el-option>
            <el-option label="☆☆☆☆ 优秀" :value="4"></el-option>
            <el-option label="☆☆☆☆☆ 优质" :value="5"></el-option>
          </el-select>
        </el-form-item>


        <el-form-item>
          <el-button type="primary" size="mini" @click="getShoppingCartListPageBefore">查询</el-button>
          <el-button type="warning" size="mini" @click="resetQueryShoppingCart">重置</el-button>
          <el-button type="warning" size="mini" @click="openSaveShoppingCart">新增</el-button>
        </el-form-item>

      </el-form>

    </el-card>



    <el-card style="margin-top: 10px">

      <el-table
        :data="shoppingCartTable.data"
        max-height="640px"
        height="640px"
        style="width: 100%">

        <el-table-column
          prop="name"
          label="物品名称"
        >
        </el-table-column>

        <el-table-column
          prop="shoppingCartTypeName"
          label="商品类型"
        >
        </el-table-column>

        <el-table-column
          prop="price"
          label="买入价格"
        >
        </el-table-column>

        <el-table-column
          prop="grade"
          label="评分"
        >
          <template slot-scope="scope">
            <el-rate v-model="scope.row.grade" :texts="texts" show-text></el-rate>
          </template>
        </el-table-column>


        <el-table-column
          prop="purchase"
          label="是否购买"
        >
          <template slot-scope="scope">
            <div v-if="scope.row.purchase === 2">
              <el-tag size="small" type="success">已购买</el-tag>
            </div>
            <div v-else>
              <el-tag size="small" type="">未购买</el-tag>
            </div>
          </template>
        </el-table-column>


        <el-table-column
          fixed="right"
          label="操作"
          width="180">
          <template slot-scope="scope">
            <el-button @click="toOpenShoppingCartDetailed(scope.row)" type="text" size="small"><i class="el-icon-view"></i>查看</el-button>
            <el-button @click="openUpdShoppingCart(scope.row)" type="text" size="small"><i class="el-icon-edit"></i>编辑</el-button>
            <el-button @click="delShoppingCartByIdTips(scope.row)" type="text" size="small"><i class="el-icon-delete"></i>删除</el-button>
          </template>
        </el-table-column>

      </el-table>

      <!--分页部分-->
      <el-pagination
        style="width:33%; margin-right:10px; margin-top:10px; "
        background
        layout="total, prev, pager, next, jumper"
        @current-change="shoppingCartPageCurrentChange"
        :page-size="shoppingCartTable.size"
        :current-page="shoppingCartTable.current"
        :pager-count="5"
        :total="shoppingCartTable.total">
      </el-pagination>

    </el-card>

    <ShoppingCartDetailed ref="shoppingCartDetailedRef"></ShoppingCartDetailed>
    <ShoppingCartSaveUpd ref="shoppingCartSaveUpdRef" @getShoppingCartListPage="getShoppingCartListPage"></ShoppingCartSaveUpd>

  </div>

</template>

<script>

  import http from '@/utils/http'
  import ShoppingCartSaveUpd from './ShoppingCartSaveUpd'
  import ShoppingCartDetailed from './ShoppingCartDetailed.vue'

  export default {
    name: "ShoppingCartList",
    components: {
      ShoppingCartSaveUpd,
      ShoppingCartDetailed,
    },
    data(){
      return {
        // 购物类型
        shoppingCartTypeList: [],
        // 查询参数
        searchShoppingCart: {
          name: null,
          typeId: null,
          details: null,
          purchase: null,
          grade: null,
        },
        // 表格
        shoppingCartTable: {
          // 表格数据
          data: [],
          // 每页记录数
          size: 30,
          // 当前页码
          current: 1,
          // 总记录数
          total: 0,
        },
        // 评价文字说明
        texts: ['非常差', '差', '普通', '优秀', '非常棒'],
      }
    },
    methods: {
      /**
       * 删除提示
       */
      delShoppingCartByIdTips(row){
        this.$confirm('是否删除该商品, 删除后无法恢复，是否继续?', '提示', {
          confirmButtonText: '确定删除',
          cancelButtonText: '取消删除',
          type: 'warning'
        }).then(() => {
          // 确定删除
          this.delShoppingCart(row)

        }).catch(() => {
          // 取消删除
        });
      },
      /**
       * 删除购物信息
       * @param row 当前行数据
       */
      delShoppingCart(row){
        http.post("/shopping/delShoppingCart", {
          id: row.id,
        }).then(response => {
          let res = response.data;
          if (res.status === 'error'){
            // 401是token过期跳转的
            if (res.code !== 401){
              this.$message.error(res.msg);
            }
            return
          }
          this.getShoppingCartListPage()
          this.$message.success("删除成功");

        }).catch(error => {
          this.$message.error(error.message);
        })
      },
      /**
       * 打开修改弹出框
       * @param row 当前行数据
       */
      openUpdShoppingCart(row){
        this.$refs.shoppingCartSaveUpdRef.openShoppingCartPage('upd', row, this.shoppingCartTypeList)
      },
      /**
       * 弹出商品详情页面
       */
      toOpenShoppingCartDetailed(row){
        this.$refs.shoppingCartDetailedRef.openShoppingCartDetailed(row)
      },
      /**
       * 打开添加弹出框
       */
      openSaveShoppingCart(){
        this.$refs.shoppingCartSaveUpdRef.openShoppingCartPage('save', null, this.shoppingCartTypeList)
      },
      /**
       * 字典分页被改变的时候
       * @param value 当前页
       */
      shoppingCartPageCurrentChange(value){
        this.shoppingCartTable.current = value
        this.getShoppingCartListPage()
      },
      /**
       * 重置
       */
      resetQueryShoppingCart(){
        this.searchShoppingCart = {
          name: null,
          typeId: null,
          details: null,
          purchase: null,
          grade: null,
        }
      },
      /**
       * 查询前重置分页参数
       */
      getShoppingCartListPageBefore(){
        this.shoppingCartTable.current = 1
        this.getShoppingCartListPage()
      },
      /**
       * 分页查询物品信息
       */
      getShoppingCartListPage(){
        this.searchShoppingCart.current = this.shoppingCartTable.current
        this.searchShoppingCart.size = this.shoppingCartTable.size
        http.post("/shopping/getShoppingCartListPage", this.searchShoppingCart).then(response => {
          let res = response.data;
          if (res.status === 'error'){
            // 401是token过期跳转的
            if (res.code !== 401){
              this.$message.error(res.msg);
            }
            return
          }
          // 分页数据
          let data = res.data;
          this.shoppingCartTable.total = data.total;
          this.shoppingCartTable.current = data.current;
          this.shoppingCartTable.size = data.size;
          // 处理数据
          this.shoppingCartTable.data = data.records

        }).catch(error => {
          this.$message.error(error.message);
        })
      },
      /**
       * 获取购物类型
       */
      getShoppingCartType(){
        http.post("/shopping/getShoppingCartType", {}).then(response => {
          let res = response.data;
          if (res.status === 'error'){
            // 401是token过期跳转的
            if (res.code !== 401){
              this.$message.error(res.msg);
            }
            return
          }

          this.shoppingCartTypeList = res.data

        }).catch(error => {
          this.$message.error(error.message);
        })
      },
    },
    mounted() {
      this.getShoppingCartType()
      this.getShoppingCartListPage()
    },
  }
</script>

<style scoped>

</style>
